/**
 */
package com.dingpeikeji.quickdeer.core.modules.viewspot.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.line.entity.TLine;
import com.dingpeikeji.quickdeer.core.modules.sys.service.AreaService;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.UserUtils;
import com.dingpeikeji.quickdeer.core.modules.viewspot.entity.SysViewspot;
import com.dingpeikeji.quickdeer.core.modules.viewspot.service.SysViewspotService;

import test.Pinyin4jUtil;

/**
 * 景点管理Controller
 * @author lh
 * @version 2016-05-31
 */
@Controller
@RequestMapping(value = "${adminPath}/viewspot/sysViewspot")
public class SysViewspotController extends BaseController {

	@Autowired
	private SysViewspotService sysViewspotService;
	@ModelAttribute
	public SysViewspot get(@RequestParam(required=false) String id) {
		SysViewspot entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sysViewspotService.get(id);
		}
		if (entity == null){
			entity = new SysViewspot();
		}
		return entity;
	}
	
	@RequiresPermissions("viewspot:sysViewspot:view")
	@RequestMapping(value = {"list", ""})
	public String list(SysViewspot sysViewspot, HttpServletRequest request, HttpServletResponse response, Model model) {
		
		Page<SysViewspot> page = sysViewspotService.findPage(new Page<SysViewspot>(request, response), sysViewspot); 
		model.addAttribute("page", page);
		return "modules/viewspot/sysViewspotList";
	}

	
	@RequiresPermissions("viewspot:sysViewspot:view")
	@RequestMapping(value = "form")
	public String form(SysViewspot sysViewspot, Model model) {
		model.addAttribute("sysViewspot", sysViewspot);
		return "modules/viewspot/sysViewspotForm";
	}

	@RequiresPermissions("viewspot:sysViewspot:edit")
	@RequestMapping(value = "save")
	public String save(SysViewspot sysViewspot, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, sysViewspot)){
			return form(sysViewspot, model);
		}
		//根据拼音搜索地点
		Pinyin4jUtil util = new Pinyin4jUtil();
		String zijie = sysViewspot.getSpotname();
//		String pinyin = util.getPinYin(zijie);
		String pinyinhead = util.getPinYinHeadChar(zijie);
//		String code = util.getCnASCII(pinyinhead);
		sysViewspot.setQuerycode(pinyinhead);
		sysViewspotService.save(sysViewspot);
		addMessage(redirectAttributes, "保存景点管理成功");
		return "redirect:"+Global.getAdminPath()+"/viewspot/sysViewspot/?repage";
	}
	
	@RequiresPermissions("viewspot:sysViewspot:edit")
	@RequestMapping(value = "delete")
	public String delete(SysViewspot sysViewspot, RedirectAttributes redirectAttributes) {
		sysViewspotService.delete(sysViewspot);
		addMessage(redirectAttributes, "删除景点管理成功");
		return "redirect:"+Global.getAdminPath()+"/viewspot/sysViewspot/?repage";
	}

}
